package com.imooc.controller;

import com.imooc.service.BuyerService;
import com.imooc.utils.ResultVOUtils;
import com.imooc.VO.ResultVO;
import com.imooc.converter.OrderForm2OrderDTOConverter;
import com.imooc.dto.OrderDTO;
import com.imooc.enums.ResultEnum;
import com.imooc.exception.SellException;
import com.imooc.form.OrderForm;
import com.imooc.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private BuyerService buyerService;

    //    创建订单
    /**这个方法参数传递还是有一些问题的*/

    @PostMapping("/create")
    public ResultVO< Map<String, String> > create(@Valid OrderForm orderForm,
                                                  BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.info("【创建订单】参数不正确，orderForm={}", orderForm);

            //希望能够提示相应的错误
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }

        OrderDTO orderDTO = OrderForm2OrderDTOConverter.convert(orderForm);
        if(CollectionUtils.isEmpty(orderDTO.getOrderDetailList())){
            log.error("【创建订单】购物车不能为空");
            throw new SellException(ResultEnum.CART_EMPTY);
        }

        //这里是转换的东西
        OrderDTO createResult = orderService.create(orderDTO);
        Map<String, String> map = new HashMap<>();
        map.put("orderId", createResult.getOrderId());
        return ResultVOUtils.success(map);
    }


    //订单列表
    @GetMapping("/list")
    public ResultVO<List<OrderDTO>> list(@RequestParam("openid") String openid,
                                         @RequestParam(value = "page", defaultValue = "0") Integer page,
                                         @RequestParam(value = "size", defaultValue = "5") Integer size) {
        if (StringUtils.isEmpty(openid)) {
            log.error("【查询订单列表】openid为空");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }

        PageRequest request = new PageRequest(page, size);
        Page<OrderDTO> orderDTOPage = orderService.findList(openid, request);

        return ResultVOUtils.success(orderDTOPage.getContent());
    }


    //    订单详情 -- 戴上了订单详情
   @GetMapping("/detail")
    public ResultVO <OrderDTO> detail(@RequestParam("openid") String openid,
                                      @RequestParam("orderId") String orderId){

        // 不安全的做法 -- 我们要增加验证openid 的方法--找到订单的方法。
       OrderDTO orderDTO  = buyerService.findOrderOne(openid, orderId);

//       OrderDTO orderDTO = orderService.findOne(orderId);
        return ResultVOUtils.success(orderDTO);
   }

//    取消订单
    @PostMapping("/cancel")
    public ResultVO cancel(@RequestParam("openid") String openid,
                           @RequestParam("orderid") String orderId){
        //改进，不安全
        OrderDTO orderDTO = new OrderDTO();             //orderService.cancel(orderDTO);
        orderDTO = buyerService.cancelOrder(openid, orderId);
        return ResultVOUtils.success();

    }


}

